#!/bin/bash
#Centos7安装LNMP环境
#注意：安装过程中会卸载系统的mariadb
#2021-11-8

DIR=$(pwd)

Check()
{
#检查下载文件
if [ ! -f "/etc/yum.repos.d/Centos-7.repo" ] && [ ! -f "/etc/yum.repos.d/epel.repo" ];then
		Prepare
fi
if [ ! -f "/opt/LNMP/nginx-1.20.1.tar.gz" ];then
		Download_nginx
fi		
if [ ! -f "/opt/LNMP/mysql-8.0.27-1.el7.x86_64.rpm-bundle.tar" ];then
		Download_mysql
fi
if [ ! -f "/opt/LNMP/php-8.0.12.tar.gz" ];then
		Download_php
fi
#检查安装情况
curl localhost | grep 'license@php.net' &> /dev/null
if [ $? == 0 ]; then
  echo -e "\n\033[32m 系统已安装LNMP \033[0m";exit 1;
  else
  mysql -V | grep 'mysql  Ver 8.0.27' &> /dev/null
	if [ $? == 0 ]; then 
		echo -e "\n\033[32m 系统已安装Mysql，开始安装php \033[0m";Install_Php
		else
		curl localhost | grep 'Welcome to nginx!' &> /dev/null
		if [ $? == 0 ]; then
			echo -e "\n\033[32m 系统已安装nginx，开始安装Mysql \033[0m";Install_Mysql
			else
			Install_Nginx
		fi
	fi
fi
}

Prepare()
{
curl -o /etc/yum.repos.d/CentOS7-aliyun.repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache
yum install -y wget gcc gcc-c++ make autoconf pcre pcre-devel zlib zlib-devel libxml2-devel krb5-devel sqlite-devel libcurl-devel openssl-devel libpng-devel epel-release
yum -y install oniguruma oniguruma-devel
mkdir -p /opt/LNMP
}

Download_nginx()
{
wget -P /opt/LNMP http://nginx.org/download/nginx-1.20.1.tar.gz
}

Download_mysql()
{
wget -P /opt/LNMP https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.27-1.el7.x86_64.rpm-bundle.tar
}

Download_php()
{
wget --no-check-certificate -P /opt/LNMP https://www.php.net/distributions/php-8.0.12.tar.gz
}

Install_Nginx()
{
tar -zxvf /opt/LNMP/nginx-1.20.1.tar.gz -C /opt/LNMP
cd /opt/LNMP/nginx-1.20.1
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make -j4 && make install
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
echo -e '
[Unit]
Description=nginx service
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
' > /usr/lib/systemd/system/nginx.service
systemctl enable nginx
systemctl restart nginx
sleep 3
curl localhost | grep 'Welcome to nginx!' &> /dev/null
if [ $? == 0 ]; 
then 
   echo -e "\n\033[32m Nginx安装成功,开始安装Mysql \033[0m";
   else
   echo -e "\n\033[31m Nginx安装失败 \033[0m"; exit 1;
fi
Install_Mysql
}

Install_Mysql()
{

tar -xvf /opt/LNMP/mysql-8.0.27-1.el7.x86_64.rpm-bundle.tar -C /opt/LNMP
cd /opt/LNMP/
yum -y remove mariadb mariadb-libs
yum -y localinstall mysql-community-common-8.0.27-1.el7.x86_64.rpm
yum -y localinstall mysql-community-client-plugins-8.0.27-1.el7.x86_64.rpm
yum -y localinstall mysql-community-libs-8.0.27-1.el7.x86_64.rpm 
yum -y localinstall mysql-community-client-8.0.27-1.el7.x86_64.rpm 
yum -y localinstall mysql-community-server-8.0.27-1.el7.x86_64.rpm
systemctl enable mysqld
systemctl start mysqld
mysqld --initialize
passwd=`grep 'password' /var/log/mysqld.log|awk '{print $NF}'`
mysqladmin -u root -p$passwd password Aa123456.
mysql -V | grep 'mysql  Ver 8.0.27' &> /dev/null
if [ $? == 0 ]; 
then 
   echo -e "\n\033[32m Mysql安装成功,开始安装Php \033[0m";
   else
   echo -e "\n\033[31m Mysql安装失败 \033[0m"; exit 1;
fi
Install_Php
}

Install_Php()
{

tar -zxvf /opt/LNMP/php-8.0.12.tar.gz -C /opt/LNMP
cd /opt/LNMP/php-8.0.12/

./configure --enable-fpm   \
--enable-mbstring  \
--enable-ftp  \
--enable-gd   \
--enable-gd-jis-conv \
--enable-mysqlnd \
--enable-pdo   \
--enable-sockets   \
--enable-xml  \
--enable-soap  \
--enable-pcntl   \
--enable-cli   \
--with-openssl  \
--with-mysqli=mysqlnd   \
--with-pdo-mysql=mysqlnd   \
--with-pear   \
--with-zlib  \
--with-iconv  \
--with-curl

make -j4 && make install
groupadd www
useradd -g www www
cp /usr/local/etc/php-fpm.conf.default /usr/local/etc/php-fpm.conf
cp php.ini-development /usr/local/php/php.ini
cp /usr/local/etc/php-fpm.d/www.conf.default /usr/local/etc/php-fpm.d/www.conf
cp sapi/fpm/php-fpm /usr/local/bin
sed -i 's/cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /usr/local/php/php.ini
sed -i 's/user = nobody/user = www/g' /usr/local/etc/php-fpm.d/www.conf
sed -i 's/group = nobody/group = www/g' /usr/local/etc/php-fpm.d/www.conf
sed -i 's/include=NONE\/etc/include=etc/g' /usr/local/etc/php-fpm.conf
sed -i 's/index  index.html index.htm;/index  index.php index.html index.htm;/g' /usr/local/nginx/conf/nginx.conf
sed -i '/deny access to .htaccess files, if Apache/i location ~* \\.php$ {\n    fastcgi_index  index.php;\n    fastcgi_pass   127.0.0.1:9000;\n    include        fastcgi_params;\n    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;\n    fastcgi_param  SCRIPT_NAME      $fastcgi_script_name;\n}' /usr/local/nginx/conf/nginx.conf
mv /usr/local/nginx/html/index.html /usr/local/nginx/html/index.html.bak
echo "<?php phpinfo(); ?>" >> /usr/local/nginx/html/index.php

echo -e '
[Unit]
Description=php-fpm
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/bin/php-fpm
PrivateTmp=true

[Install]
WantedBy=multi-user.target
' > /usr/lib/systemd/system/php-fpm.service

systemctl enable php-fpm
systemctl start php-fpm
systemctl restart nginx
sleep 3

curl localhost | grep 'license@php.net' &> /dev/null
if [ $? == 0 ]; 
then 
   echo -e "\n\033[32m Php安装成功 \033[0m";
   else
   echo -e "\n\033[31m Php安装失败 \033[0m"; exit 1;
fi
End
}

End()
{
systemctl status firewalld |grep 'Active: active (running)' &> /dev/null
if [ $? == 0 ];
then
    echo -e "\n\033[32m 添加防火墙规则 \033[0m";
    firewall-cmd --zone=public --add-port=80/tcp --permanent
    firewall-cmd --reload
fi

echo -e "\n\033[32m--LNMP搭建完成，请使用浏览器访问 http:serverIP 测试\n--软件版本如下：\033[0m\n"
nginx -v
echo -e "\033[36m---------------------------------------- \033[0m"
mysql -V
echo -e "\033[36m---------------------------------------- \033[0m"
php -v
echo -e "\033[36m---------------------------------------- \033[0m"
echo -e "\n\033[32m mysql root密码：Aa123456. \033[0m\n"
echo -e "\033[36m---------------------------------------- \033[0m"

cd $DIR
#rm -rf /opt/LNMP
#rm -rf install-LNMP.el7.sh
exit
}


Check
Prepare
Install_Nginx
Install_Mysql
Install_Php
End
